package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.SysNotice;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysNoticeMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysNoticeService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统消息通知表
 *
 * @author miracle_j
 * @date 2022年11月06日 21:34:52
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private ISysNoticeMapper sysNoticeMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(SysNotice req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysNotice req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysNoticeMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysNotice sysNotice) {
        WJAssert.notNull(sysNotice, "入参对象不能为空");
        WJAssert.notNull(sysNotice.getId(), "id参数错误");
        SysNotice where = new SysNotice();
        where.setId(sysNotice.getId());
        return sysNoticeMapper.updateSelective(sysNotice, where);
    }

    @Override
    public int getCount(SysNotice sysNotice) {
        checkStringLength(sysNotice, true);
        return sysNoticeMapper.getCount(sysNotice);
    }

    @Override
    public SysNotice getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysNotice where = new SysNotice();
        where.setId(id);
        return sysNoticeMapper.getOne(where);
    }

    @Override
    public List<SysNotice> getList(SysNotice sysNotice) {
        checkStringLength(sysNotice, true);
        return sysNoticeMapper.getList(sysNotice);
    }

    @Override
    public List<SysNotice> getPageList(SysNotice sysNotice) {
        checkStringLength(sysNotice, true);
        return sysNoticeMapper.getPageList(sysNotice);
    }

    @Override
    public void audit(Long id, String operator) {
        SysNotice req = new SysNotice();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysNotice req = new SysNotice();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysNotice req = new SysNotice();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void oneClickRead(Long userId) {
        WJAssert.notNull(userId, "参数不合法");
        sysNoticeMapper.oneClickRead(userId);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysNotice sysNotice, boolean nullAble) {
        WJAssert.notNull(sysNotice, "入参对象不能为空");
        WJAssert.limitMaxLength(sysNotice.getTitle(), nullAble, 50, "通知标题不合法!");
        WJAssert.limitMaxLength(sysNotice.getContent(), true, 255, "通知内容不合法!");
        WJAssert.limitMaxLength(sysNotice.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(sysNotice.getRemark(), true, 255, "备注不合法!");
    }
}
